package com.xxp.controller;

import com.xxp.common.Result;
import com.xxp.controller.DTO.LoginDto;
import com.xxp.controller.request.AdminPageRequest;
import com.xxp.controller.request.LoginRequest;
import com.xxp.controller.request.PassRequest;
import com.xxp.controller.request.UserPageRequest;
import com.xxp.entity.Admin;
import com.xxp.entity.User;
import com.xxp.service.impl.AdminServiceImpl;
import com.xxp.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin    //跨域解决方法
public class AdminController {
    @Autowired
    private AdminServiceImpl adminService;

    @PutMapping("/password")
    public Result changePass(@RequestBody PassRequest passRequest){
        adminService.changePass(passRequest);
        return Result.success();
    }
    @PutMapping("/pass")
    public Result pass(@RequestBody PassRequest passRequest){
        adminService.changePassById(passRequest);
        return Result.success();
    }
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request){
        LoginDto login = adminService.login(request);
        return Result.success(login);
    }
    @PostMapping("/add")
    public Result save(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.success();
    }
    @GetMapping("/list")//查询所有
    public Result list(){
        List<Admin> admin = adminService.list();
        return Result.success(admin);
    }
    @GetMapping("/page")//分页模糊查询
    public Result page(AdminPageRequest adminPageRequest){
        return Result.success(adminService.page(adminPageRequest));
    }
    @GetMapping("/{id}")//根据Id查询
    public Result getById(@PathVariable Integer id){
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }
    @PutMapping("/update")//根据ID修改
    public Result update(@RequestBody Admin admin){
        adminService.update(admin);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        adminService.deleteById(id);
        return Result.success();
    }
}
